﻿using System;
using System.IO;
using System.Web;
using AFT.Parking.Entity.ArgumentEntity;
using AFT.Parking.Foundation;

namespace AFT.Parking.WebSite.Common
{
    public static class FileHelper
    {
        /// <summary>
        /// 上传文件,返回result,arr[0]:临时地址;arr[1]:真实地址;arr[2]:文件名;arr[3]:文件大小，单位M
        /// </summary>
        /// <param name="appName">文件夹名</param>
        /// <returns></returns>
        public static BoolResult Upload(string appName)
        {
            var operationResult = new BoolResult();

            string param = string.Format("{1}/{0}", DateTime.Now.ToString("yyyy/MM"), appName);
            try
            {
                string path = UploadFile.Upload(appName, param);
                if (!string.IsNullOrEmpty(path))
                {
                    operationResult.Value = true;
                    string[] imgs = path.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    string[] arr = new string[4];
                    if (imgs.Length > 0)
                    {
                        arr[0] = imgs[0];
                        arr[1] = UploadFile.GetDbPath(imgs[0]);
                        arr[2] = HttpContext.Current.Request.Files[0].FileName;
                        arr[3] = (HttpContext.Current.Request.Files[0].ContentLength / 1024.0 / 1024.0).ToString();
                    }
                    operationResult.Result = arr;
                }
                else
                {
                    operationResult.Value = false;
                }
            }
            catch (Exception ex)
            {
                operationResult.Value = false;
                operationResult.Message = ex.Message;
            }
            return operationResult;
        }
        public static string Save(string appName, string tempPath)
        {
            return UploadFile.Save(appName, tempPath);
        }
      
        private static string GetPhysicsPath(string path)
        {
            return HttpContext.Current.Server.MapPath(path);
        }

        private static string GetFolder()
        {
            string filePath = string.Format("/TempFile/WaterImage/{0}/", DateTime.Now.ToString("yyyy-MM-dd"));
            if (!Directory.Exists(GetPhysicsPath(filePath)))
                Directory.CreateDirectory(GetPhysicsPath(filePath));
            return filePath;
        }

        /// <summary>
        /// 获取缩略图
        /// </summary>
        /// <param name="fullName">原始文件名</param>
        /// <param name="mark">缩略图标识名称</param>
        /// <returns></returns>
        public static string GetThumbnail(string fullName, string mark)
        {
            if (!string.IsNullOrEmpty(fullName) && !string.IsNullOrEmpty(mark))
            {
                int pos = fullName.LastIndexOf(".");
                if (pos != -1)
                {
                    return string.Format("{0}{1}{2}", fullName.Substring(0, pos), mark,
                        fullName.Substring(pos, fullName.Length - pos));
                }
            }
            return ConstFields.DefaultImage;
        }

    }
}